package com.huanletao.quartz.execute;

import com.huanletao.quartz.entity.JobDetails;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 由这个executeJob 调用实际的方法；
 *
 * </p>
 *
 * @package: com.huanletao.quartz
 * @description:
 * @className: ExecuteJobQuartz
 * @author: @nncskj admin
 * @date: Created in 2022/5/31
 * @copyright: Copyright (c) 2022/5/31
 * @version: V1.0
 */
public abstract class AbstractExecuteJobQuartz implements Job {

    private static Logger logger = LoggerFactory.getLogger(AbstractExecuteJobQuartz.class);

    /**
     * @param jobExecutionContext
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        JobDetails jobDetails = (JobDetails) jobDataMap.get("jobDetails");
        before(jobDetails);
        logger.info("定时开始执行");
        long currentTimeMillis = System.currentTimeMillis();
        try {
            doExecute(jobDetails);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            after(jobDetails);
        }
        logger.info("定时运行完成 消耗时间:{},", (System.currentTimeMillis() - currentTimeMillis));
    }

    /**
     * @param jobDetails
     */
    public abstract void before(JobDetails jobDetails);

    /**
     *
     * @param details
     */
    public abstract void doExecute(JobDetails details) throws Exception;

    /**
     *
     * @param jobDetails
     */
    public abstract void after(JobDetails jobDetails);
}
